/**
 * 图表订单统计控制器
 */
import tem from './index.html'
import $_fn from '~plugins'
import G2 from 'g2'
class  chartsOrderCtrl {
    constructor($scope) {
    	$scope.fn=function(list){
    		/*var data = [
            {"state":"待提交","normal":25,"reject":0},
            {"state":"审批中","normal":122,"reject":25},
            {"state":"合同管理","normal":258,"reject":0},
            {"state":"等待放款","normal":42,"reject":0},
            {"state":"正常放款","normal":54,"reject":0},
            {"state":"已逾期","normal":300,"reject":0}
            ];*/
          var datas=list.data.orderStatus 
          var data=[];
          if(datas && datas.length){
		      for(var j=0; j<datas.length;j++ ){
		      	data[j]={}
		      	data[j].state=datas[j].s_name;
		      	data[j].normal=datas[j].count;
		      	data[j].reject=datas[j].back || 0;
		      }
	      }
          console.log('订单状态------',data)
        var Stat = G2.Stat;
        var Frame = G2.Frame;
        var frame = new Frame(data);
        frame = Frame.combinColumns(frame,['normal', 'reject'],'data', 'sta', 'state');
        frame.addCol('type', function(obj) {
            var key = obj.sta;
            var type;
            if (key === 'normal') {
                type = '正常';
            } else {
                type = '拒绝';
            }
            return type;
        });
        var colorMap = {
            'normal': '#7bce7d',
            'reject': '#f16d60'
        }
        var chart = new G2.Chart({
            id : 'charts_order',
            forceFit: true,
            height: 450,
            plotCfg: {
                margin: [50, 0, 80,50]
            }
        });

        // chart.legend(false);
        chart.source(frame, {
            state: {
                alias: '订单统计'
            }
        });
        chart.axis('state', {
            title: {
                fontSize: '14',
                fill: '#333',
            },
            labels:{
                label: {
                    fontSize: '13', // 文本大小
                }
            }
        });
        chart.axis('data', {
            title:null,
            labels:{
                label: {
                    fontSize: '14', // 文本大小
                }
            }
        });
        chart.interval(['dodge', 'stack']).position('state*data').color('type*sta', function(type, sta) {
            return colorMap[sta];
        }).size(20)
        chart.render();
    	}
    	$scope.kongZhiTai($scope.fn)
    	
    	
    	
    	
    	
    	
        /*var data = [
            {"state":"待提交","normal":25,"reject":0},
            {"state":"审批中","normal":122,"reject":25},
            {"state":"合同管理","normal":258,"reject":0},
            {"state":"等待放款","normal":42,"reject":0},
            {"state":"正常放款","normal":54,"reject":0},
            {"state":"已逾期","normal":300,"reject":0}
            ];
        var Stat = G2.Stat;
        var Frame = G2.Frame;
        var frame = new Frame(data);
        frame = Frame.combinColumns(frame,['normal', 'reject'],'data', 'sta', 'state');
        frame.addCol('type', function(obj) {
            var key = obj.sta;
            var type;
            if (key === 'normal') {
                type = '正常';
            } else {
                type = '拒绝';
            }
            return type;
        });
        var colorMap = {
            'normal': '#7bce7d',
            'reject': '#f16d60'
        }
        var chart = new G2.Chart({
            id : 'charts_order',
            forceFit: true,
            height: 450,
            plotCfg: {
                margin: [50, 0, 80,50]
            }
        });

        // chart.legend(false);
        chart.source(frame, {
            state: {
                alias: '订单统计'
            }
        });
        chart.axis('state', {
            title: {
                fontSize: '14',
                fill: '#333',
            },
            labels:{
                label: {
                    fontSize: '13', // 文本大小
                }
            }
        });
        chart.axis('data', {
            title:null,
            labels:{
                label: {
                    fontSize: '14', // 文本大小
                }
            }
        });
        chart.interval(['dodge', 'stack']).position('state*data').color('type*sta', function(type, sta) {
            return colorMap[sta];
        }).size(20)
        chart.render();*/
    }
}


chartsOrderCtrl.$inject = ['$scope','$rootScope','$timeout','$location','$interval']
module.exports = angular.module('directive.chartsOrder', []).directive('chartsOrder', function() {
    return {
        restrict: 'E',
        template: tem,
        replace:true,
        controller: chartsOrderCtrl
    }
}).name